package com.xquant.trade.common.utils;


import cn.hutool.json.JSONUtil;
import com.xquant.common.core.constant.Constants;
import com.xquant.common.core.domain.Underlying;
import com.xquant.common.core.utils.SpringUtils;
import com.xquant.common.redis.service.RedisService;
import org.springframework.data.redis.core.Cursor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Redis标的参数工具类
 *
 * @author xquant
 */
public class UnderlyingUtils {

    private static int num = 2;


    /**
     * 设置标的参数缓存
     *
     * @param key 参数键
     */
    public static void setCache(String key, Underlying param) {
        SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(key), param);
    }

    /**
     * 获取标的参数缓存
     *
     * @param key 参数键
     * @return dictDatas 标的参数数据列表
     */
    public static Underlying getCache(String key) {
        Object cacheObj = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
        if (StringUtils.isNotNull(cacheObj)) {
            Underlying underlying = JSONUtil.toBean(JSONUtil.parseObj(cacheObj), Underlying.class);
            return underlying;
        }
        return null;
    }

    /**
     * 获取标的参数缓存 模糊查询
     *
     * @param condition 参数键 string模式
     * @return dictDatas 标的参数数据列表
     */
    public static List<Underlying> getDimCache(String condition) {
        List<Underlying> underlyingList = new ArrayList<>();
        List<String> list = SpringUtils.getBean(RedisService.class).getDimCacheObject(getCacheKey(condition));
        if (list == null) {
            return underlyingList;
        }
        if (list.size() != 0) {
            if (list.size() > 15) {
                list = list.subList(0, 15);
            }
            for (String key : list) {
                //通过查到的key值获取value，并放入result
                Object cacheObj = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
                if (StringUtils.isNotNull(cacheObj)) {
                    Underlying underlying = JSONUtil.toBean(JSONUtil.parseObj(cacheObj), Underlying.class);
                    underlyingList.add(underlying);
                }

            }
        }
        return underlyingList;
    }

    /**
     * 获取标的参数缓存 模糊查询
     *
     * @param condition 参数键 hash 模式
     * @return dictDatas 标的参数数据列表
     */
    public static List<Underlying> getDimHCache(String condition, String aType) {
        List<Underlying> underlyingList = new ArrayList<>();
        List<Underlying> listStr = getDimHCacheObject(Constants.SYS_UNDERLYING_PRICING_KEY, condition);

        return listStr;
    }

    /**
     * 获得模糊匹配缓存的基本对象。
     *
     * @param
     * @return 缓存键值对应的数据 hash
     */
    public static List<Underlying> getDimHCacheObject(String field, String str) {
        List<Underlying> list = new ArrayList<>();

        Cursor<Map.Entry<Object, Object>> cursor = SpringUtils.getBean(RedisService.class).getDimHashCacheObject(field, str);
        if (cursor == null) {
            return list;
        }
        while (cursor.hasNext()) {
            if (list.size() >= 15) {
                break;
            }
            Map.Entry<Object, Object> entry = cursor.next();
            Object key = entry.getKey();
            Object valueSet = entry.getValue();
            list.add((Underlying) valueSet);
        }
        //关闭cursor
        cursor.close();
        return list;
    }

    /**
     * 根据标的参数类型和标的参数值获取标的参数标签
     *
     * @param key 标的参数值
     * @return 标的参数标签
     */
    public static String getValue(String key) {
        if (StringUtils.isNotEmpty(key)) {
            Underlying underlying = getCache(key);
            if (underlying != null) {
                return underlying.getiName();
            }
        }
        return key;
    }

    /**
     * 根据标的参数类型和标的参数标签获取标的参数值
     *
     * @param value 标的参数标签
     * @return 标的参数值
     */
    public static String getKey(String value) {
        if (StringUtils.isNotEmpty(value)) {
            Underlying param = getCache(value);
            if (param != null) {
                return param.getCode();
            }
        }
        return value;
    }

    /**
     * 清空标的参数缓存
     */
    public static void clearCache() {
        Collection<String> keys = SpringUtils.getBean(RedisService.class).keys(Constants.SYS_UNDERLYING_PRICING_KEY + "*");
        SpringUtils.getBean(RedisService.class).deleteObject(keys);
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey) {
        return Constants.SYS_UNDERLYING_PRICING_KEY + configKey;
    }
}
